package com.ksd1.notes;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ksd1.notes.db.NoteDB;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 底部弹出框
 */
public class InputBottom extends DialogFragment {
    @BindView(R.id.editText)
    EditText mcontent;
    @BindView(R.id.finish)
    TextView btnfinish;//礼物名字


    String msg = "";
    int position = -1;
    String errorMsg = "保存失败";//出错提示
    MainActivity parent;

    @Nullable
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        parent = ((MainActivity)getActivity());
        // 使用不带Theme的构造器, 获得的dialog边框距离屏幕仍有几毫米的缝隙。
        Dialog dialog = new Dialog(getActivity(), R.style.BottomDialog);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        dialog.setContentView(R.layout.fragment_input_bottom);
        dialog.setCanceledOnTouchOutside(true); // 外部点击取消

        // 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);

        ButterKnife.bind(this, dialog); // Dialog即View

        //获取传递过来的数据
        Bundle bundle = getArguments();
        if(bundle!=null){
            msg = bundle.getString("msg", "");
            position = bundle.getInt("position", -1);
        }

        initView();
        initClickTypes();
        showSoftInputFromWindow(getActivity(), mcontent);

        return dialog;
    }

    /**
    * 初始化视图
    */
    protected void initView(){
        mcontent.setText(msg);
    }

    /**
    * 添加监听事件
    */
    protected void initClickTypes() {
        btnfinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!saveData()){
                    Toast.makeText(v.getContext(), errorMsg, Toast.LENGTH_SHORT).show();
                }

                InputBottom.this.dismiss();
            }
        });
    }


    /**
     * 将数据保存到数据库
     */
    protected boolean saveData(){
        String text = mcontent.getText().toString();
        if(text.equals("")){
            return true;//空消息不保存，直接return
        }

//        Log.d("20182005085", "saveData: "+position);

        if(position != -1){//保存旧消息
            if(parent != null){
//                Log.d("20182005085", "saveData: "+parent.todoList.get(position).status);
                parent.todoList.get(position).content = text;
                parent.adapter.notifyItemChanged(position);
            }
        }else{//保存新消息
            if(parent != null){
                parent.todoList.add(0, new NoteDB.Todo(text, 0));
                parent.cnt++;
                parent.todoList.get(parent.cnt).status = 0;
//                Log.d("20182005085", "saveData: "+parent.todoList.get(0).status);
                parent.adapter.notifyItemInserted(0);
//                parent.adapter.notifyItemRangeChanged(0, parent.todoList.size());
            }
        }
        parent.todo_recyclerView.scrollToPosition(0);
        return true;
    }

    /**
     * 添加一条新消息后，往后移动
     */
    /*protected void operator(){
        if(parent==null) return;
        List<NoteDB.Todo> list = parent.todoList;

        for(int i=list.size();i>=1;i--){
            list.get(i).content = list.get(i-1).content;
            list.get(i).status = list.get(i-1).status;
        }
    }*/


    /**
     * EditText获取焦点并显示软键盘
     */
    public static void showSoftInputFromWindow(Activity activity, EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }


    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
}
